#include <QtCore>

#include "scooper.hpp"
#include "stopwatch.hpp"
#include "logger.hpp"

int main(int argc,char *argv[]) {
  if (argc!=3) {
    printf("Usage: voxelscoop in_file out_file.tree\n   or: voxelscoop input_folder output_folder\n");
    exit(1);
  }
  Stopwatch sw;
  Logger logger;
  QString input(argv[1]);
  QString output(argv[2]);
  QFileInfo inputFileInfo(input);
  QFileInfo outputFileInfo(output);
  if (inputFileInfo.isFile() && outputFileInfo.suffix()==QString("tree")) {
    QString fileName=inputFileInfo.absoluteFilePath();
    Volume vol(fileName,false,false);
    Scooper scooper(&vol);
    sw.start();
    scooper.scoop();
    sw.stop();
    sw.print("Scooped");
    logger << sw.toString("Scooped") << endl;
    QString outFileName=outputFileInfo.absoluteFilePath();
    scooper.write(outFileName);
  } else if (inputFileInfo.isDir() && outputFileInfo.isDir()) {
    QDir inputDir(input);
    QDir outputDir(output);
    inputDir.setFilter(QDir::Files);
    QStringList nameFilters;
    nameFilters << "*.rawx" << "*.zrawx";
    inputDir.setNameFilters(nameFilters);
    inputDir.setSorting(QDir::Name);
    QStringList files=inputDir.entryList();
    if (files.size()>0) {
      QStringListIterator i(files);
      while (i.hasNext()) {
        QString fileName=inputDir.absoluteFilePath(i.next());
        Volume *vol=new Volume(fileName,false,false);
        Scooper scooper(vol);
        sw.start();
        scooper.scoop();
        sw.stop();
        sw.print("Scooped");
        logger << sw.toString("Scooped") << endl;
        QString outFileName=outputDir.absoluteFilePath(QFileInfo(fileName).baseName().append(".tree"));
        scooper.write(outFileName);
        delete vol;
      }
    }   
  } else {
    printf("Not valid input.\n");
    exit(1);
  }
  logger.save(QString("%1.log").arg(outputFileInfo.baseName()));
}
